package com.dqp.cslggroup.MenuViewPage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.Util.TimeTools;
import com.dqp.cslggroup.bean.note;

import java.io.File;
import java.util.List;

public class ClassNoteAdapter extends BaseAdapter {

    private List<note> notes;
    private LayoutInflater inflater;
    private Context context;

    public ClassNoteAdapter(List<note> notes, Context context) {
        this.notes = notes;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return notes == null ? 0 : notes.size();
    }

    @Override
    public Object getItem(int position) {
        return notes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        @SuppressLint({"ViewHolder", "InflateParams"}) View view = inflater.inflate(R.layout.college_item, null);
        note note = (note) getItem(position);
        TextView name = view.findViewById(R.id.college_name);
        TextView des = view.findViewById(R.id.college_des);
        TextView location = view.findViewById(R.id.college_profession);
        ImageView img = view.findViewById(R.id.img);
        TextView alarm = view.findViewById(R.id.college_pv);
        des.setMaxLines(10);
        name.setText(note.getName() + "");
        alarm.setText(note.getData());
        des.setText(note.getText());
        if (note.getWeek() - 1 >= 0 && note.getWeek() - 1 <= 6)
            location.setText(TimeTools.toxinqi(note.getWeek() - 1) + "|第" + note.getStart() + "节");
        if (note.getImg() != null) {
            Glide.with(context).load(note.getImg()).into(img);
            img.setVisibility(View.VISIBLE);
            img.setOnClickListener(v -> {
                StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
                StrictMode.setVmPolicy(builder.build());
                builder.detectFileUriExposure();
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(new File(note.getImg())), "image/*");
                context.startActivity(intent);
            });
        }
        return view;
    }

}
